# @Time    : 2018/5/30 17:46
# @Author  : Jame
from interface import admin_interface,common_interface
from lib import  common
from conf import setting
import os




admin_info={
    'name':None,
    'role':'admin'
}



def admin_register():
    print('管理员注册>>>')
    while True:
        name=input('请输入注册的用户名:').strip()
        password=input('请输入注册的密码:').strip()
        conf_password=input('请再次输入注册的密码:').strip()
        if password==conf_password:
            flag,msg=admin_interface.admin_register_interface(name,password)
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print('两次密码不一致，请重新输入.')




def admin_login():
    print('管理员登陆>>>')
    while True:
        name=input('请输入用户名:').strip()
        if admin_info['name'] == name:
            print('您已登陆，请勿重复重登录...')
            break

        password=input('请输入密码:').strip()
        flag,msg=common_interface.login(name,password,admin_info['role'])
        if flag:
            print(msg)
            admin_info['name']=name
            break
        else:
            print(msg)



@common.auth_type(admin_info['role'])
def admin_create_school():
    print('管理员创建学校>>>')
    while True:
        school_name=input('请输入学校名称:').strip()
        school_address=input('请输入学校地址:').strip()
        flag,msg=admin_interface.admin_create_school(admin_info['name'],school_name,school_address)
        if flag:
            print(msg)
            break
        else:
            print(msg)




@common.auth_type(admin_info['role'])
def admin_create_teacher():
    print('管理员创建老师>>>')
    while True:
        teacher_name = input('请输入老师的用户名:').strip()
        teacher_password = input('请输入老师的密码:').strip()
        conf_teacher_password=input('请再次输入老师的密码:').strip()
        if teacher_password==conf_teacher_password:
            flag1,msg1=common_interface.chose_school()
            if flag1:
                school_name=msg1
            else:
                print(msg1)
                break

            flag2,msg2 = admin_interface.admin_create_teacher(admin_info['name'],teacher_name,teacher_password,school_name)
            if flag2:
                print(msg2)
                break
            else:
                print(msg2)
        else:
            print('两次密码不一致，请重新输入...')




@common.auth_type(admin_info['role'])
def admin_create_course():
    print('管理员创建课程>>>')
    while True:
        course_name = input('请输入课程名称:').strip()
        cyclical=input('请输入课程周期:').strip()
        price=input('请输入课程价格:').strip()

        flag1, msg1 = common_interface.chose_school()
        if flag1:
            school_name = msg1
        else:
            print(msg1)
            break

        flag2,msg2 = admin_interface.admin_create_course(admin_info['name'],course_name,cyclical,price,school_name)
        if flag2:
            print(msg2)
            break
        else:
            print(msg2)


@common.auth_type(admin_info['role'])
def admin_check_log():
    log_path=os.path.join(setting.LOG_DIR,'access.log')
    while True:
        n=input('请输入查看最后几行(1-n为数字):').strip()
        if not n.isdigit():continue
        n=int(n)
        with open(log_path,encoding='utf-8') as f:
            txt = f.readlines()
        keys = [k for k in range(0, len(txt))]
        result = {k: v for k, v in zip(keys, txt[::-1])}
        for i in range(n):
            print(result[i])
        break



@common.auth_type(admin_info['role'])
def modify_password():
    print('修改我的密码')
    while True:
        newpassword=input('请输入新密码:').strip()
        conf_newpassword=input('再次确认新密码:').strip()
        if newpassword==conf_newpassword:
            flag,msg=admin_interface.modify_passwod_interface(admin_info['name'],newpassword)
            if flag:
                print(msg)
                admin_info['name']=None
                admin_login()
                break
            else:
                print(msg)
                break
        else:
            print('两次输入的密码不一致，请重新输入..')

@common.auth_type(admin_info['role'])
def modify_teacher_password():
    print('修改老师的密码')
    while True:
        teacher_name=input('请输入要修改老师的用户名:').strip()
        newpassword=input('请输入新密码:').strip()
        conf_newpassword=input('再次确认新密码:').strip()
        if newpassword==conf_newpassword:
            flag,msg=admin_interface.modify_teacher_passwod_interface(admin_info['name'],teacher_name,newpassword)
            if flag:
                print(msg)
                break
            else:
                print(msg)
                break
        else:
            print('两次输入的密码不一致，请重新输入..')

func_dic={
    '1': admin_register,
    '2': admin_login,
    '3': admin_create_school,
    '4': admin_create_teacher,
    '5': admin_create_course,
    '6': admin_check_log,
    '7':modify_password,
    '8':modify_teacher_password
}


def admin_view():
    while True:
        print('''
        1、注册
        2、登录
        3、创建学校
        4、创建老师
        5、创建课程
        6、管理员查看日志
        7、修改我的密码
        8、修改老师的密码
        ''')
        choice = input('please choice (q 退出)>>:').strip()
        if choice == 'q': break
        if choice not in func_dic:
            print('您输入的不在范围内,请重新输入.')
            continue
        else:
            func_dic[choice]()
